package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Permission;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
/**
 * @Author gentleman Ma
 * @Date created in 2020/1/3
 * @description 权限管理控制器
 * @Version 1.0
 */
@RestController
@RequestMapping("/permission")
@Slf4j
public class PermissionController {

    @Reference
    private PermissionService permissionService;


    /**
     * 添加权限
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Permission permission){
     log.info("[权限管理-新增]permission:{}",permission);
     permissionService.add(permission);
     return new Result(true, "新增权限管理成功");
    }


    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @GetMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean){
        log.info("[权限管理-分页查询]data:{}", queryPageBean);
        PageResult pageResult =permissionService.pageQuery(queryPageBean);
        return new Result(true,MessageConst.ACTION_SUCCESS,pageResult);
    }


    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer id){
        log.info("[权限管理-根据id删除]id：{}",id);
        //RPC调用处理业务
        permissionService.delete(id);
        return new Result(true,MessageConst.DELETE_CHECKITEM_SUCCESS);
    }

    /**
     * 编辑
     *
     * @param checkItem
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody Permission permission) {
        log.info("[权限管理-编辑]data:", permission);
        permissionService.edit(permission);
        return new Result(true, MessageConst.EDIT_CHECKITEM_SUCCESS);
    }


    /**
     * 根据Id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(Integer id) {
        log.info("[权限管理-根据ID查询]id:{}", id);
        Permission Permission = permissionService.findById(id);
        return new Result(true,MessageConst.ACTION_SUCCESS,Permission);
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(){
        List<Permission> permissions = permissionService.findAll();
        return new Result(true, MessageConst.ACTION_SUCCESS,permissions);
    }
}
